Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to pass a custom JSONDecoder to OnRequestHandler #155

Conversation

DominikPalo
Copy link
Contributor

This PR allows providing a custom instance of JSONEncoder while initializing the OnRequestHandler.

Motivation

Some API endpoints require all JSON keys in payloads to adhere to the snake-case naming convention. However, to be consistent with the Swift naming convention, we use camelCase naming convention for declaring properties in underlying Codable structures (JSON models). When calling the API, we configure the Alamofire key encoding strategy to snake-case (encoder.keyEncodingStrategy = .convertToSnakeCase). Unfortunately, with the current implementation, it is not possible to mock such endpoints, as the request handler always uses the default JSONDecoder() for parsing/decoding the request body.

After applying this PR, it will be possible to configure the JSONDecoder as needed; for example, in our case, to set the key decoding strategy to snake_case:

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

mock.onRequestHandler = OnRequestHandler(httpBodyType: BodyType.self, jsonDecoder: decoder) { _, body in
	// Assertions
}

Copy link
Contributor

@raphkoebraam raphkoebraam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution and for validating it with tests, @DominikPalo! 🚀

@wetransferplatform
Copy link
Collaborator

Warnings
⚠️

'onRequest' is deprecated: Use onRequestHandler instead.

⚠️

'onRequest' is deprecated: Use onRequestHandler instead.

Messages
📖 MockerTests: Executed 33 tests (0 failed, 0 retried, 0 skipped) in 1.841 seconds
📖

View more details on Bitrise

Code Coverage Report

Name Coverage
Mocker 82.35%

SwiftLint found issues

Severity File Reason
Warning OnRequestHandler.swift:30 Line should be 140 characters or less; currently it has 149 characters (line_length)
Warning OnRequestHandler.swift:56 Line should be 140 characters or less; currently it has 149 characters (line_length)
Error OnRequestHandler.swift:66 Line should be 160 characters or less; currently it has 168 characters (line_length)
Error OnRequestHandler.swift:82 Line should be 160 characters or less; currently it has 163 characters (line_length)
Warning MockerTests.swift:374 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:400 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:422 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:441 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:666 File should contain 660 lines or less: currently contains 666 (file_length)
Warning MockerTests.swift:187 Line should be 140 characters or less; currently it has 151 characters (line_length)
Warning MockerTests.swift:205 Line should be 140 characters or less; currently it has 149 characters (line_length)
Warning MockerTests.swift:263 Line should be 140 characters or less; currently it has 157 characters (line_length)
Warning MockerTests.swift:400 Line should be 140 characters or less; currently it has 145 characters (line_length)
Warning MockerTests.swift:52 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:71 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:94 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:116 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:148 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:173 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:189 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:207 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:230 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:283 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:574 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:608 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:261 Variables should not have redundant type annotation (redundant_type_annotation)
Warning MockerTests.swift:132 Use shorthand syntax for optional binding (shorthand_optional_binding)
Warning MockerTests.swift:267 Use shorthand syntax for optional binding (shorthand_optional_binding)
Warning MockerTests.swift:562 Use shorthand syntax for optional binding (shorthand_optional_binding)
Warning MockerTests.swift:152 Lines should not have trailing whitespace (trailing_whitespace)
Warning MockerTests.swift:167 Lines should not have trailing whitespace (trailing_whitespace)
Warning MockerTests.swift:15 Type body should span 400 lines or less excluding comments and whitespace: currently spans 465 lines (type_body_length)
Warning MockerTests.swift:48 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:67 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:90 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:112 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:130 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:162 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:185 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:203 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:226 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:242 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:265 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:557 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:600 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:316 Prefer the specific matcher 'XCTAssertEqual' instead (xct_specific_matcher)

Generated by 🚫 Danger Swift against 47cdf02

Copy link
Contributor

@BasThomas BasThomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 nice!

@raphkoebraam raphkoebraam merged commit f732d50 into WeTransfer:master Mar 18, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants